package com.edu.util;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.logging.Level;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.edu.exception.FileException;

public class IOHelper {
	
	
	private final static Logger logger = LoggerFactory.getLogger(IOHelper.class);
	/**
	 * 创建文件
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean createFile(File fileName) throws Exception {
		try {
			if (!fileName.exists()) {
				fileName.createNewFile();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 读取TXT内容
	 * 
	 * @param file
	 * @return
	 */
	public static String readTxtFile(File file) {
		String result = "";
		try {
			InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "gbk");
			BufferedReader br = new BufferedReader(reader);
			String s = null;
			while ((s = br.readLine()) != null) {
				result = result + s;
				System.out.println(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入TXT，覆盖原内容
	 * 
	 * @param content
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static boolean writeTxtFile(String content, File fileName) throws FileException {
		//RandomAccessFile mm = null;
		boolean flag = false;
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(fileName);
			fileOutputStream.write(content.getBytes("utf-8"));
			fileOutputStream.close();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 写入TXT，追加写入
	 * 
	 * @param filePath
	 * @param content
	 */
	public static void fileChaseFW(String filePath, String content) {
		try {
			// 构造函数中的第二个参数true表示以追加形式写文件
			FileWriter fw = new FileWriter(filePath, true);
			fw.write(content);
			fw.close();
		} catch (IOException e) {
			System.out.println("文件写入失败！" + e);
		}
	}
	
	public static byte[] fileToBytes(File file) {
        byte[] buffer = null;
        
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;

        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();

            byte[] b = new byte[1024];

            int n;

            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            
            buffer = bos.toByteArray();
        } catch (FileNotFoundException ex) {
            logger.debug(ex.getMessage());
        } catch (IOException ex) {
        	 logger.debug(ex.getMessage());
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
            } catch (IOException ex) {
            	logger.debug(ex.getMessage());
            } finally{
                try {
                    if(null!=fis){
                        fis.close();
                    }
                } catch (IOException ex) {
                	logger.debug(ex.getMessage());
                }
            }
        }
        
        return buffer;
    }

	public static void bytesToFile(byte[] buffer, final File file){


        OutputStream output = null;
        BufferedOutputStream bufferedOutput = null;

        try {
            output = new FileOutputStream(file);

            bufferedOutput = new BufferedOutputStream(output);

            bufferedOutput.write(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(null!=bufferedOutput){
                try {
                    bufferedOutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != output){
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
